package com.jiuzhe.commercial.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.jiuzhe.commercial.R;
import com.jiuzhe.commercial.bean.HouseListBean;
import com.jiuzhe.commercial.ui.house.HouseListActivity;

import java.util.HashMap;
import java.util.List;

/**
 * Created by huanghao on 2018/4/27.
 */

public class HouseListAdapter extends RecyclerView.Adapter<HouseListAdapter.ViewHolder> {
    private Context context;
    private List<HouseListBean> list;
    private HouseListActivity.MyHandler mHandler;
    private static HashMap<Integer, Boolean> isSelected = new HashMap<>();

    public HouseListAdapter(List<HouseListBean> list, HouseListActivity.MyHandler handler) {
        this.list = list;
        this.mHandler = handler;
        initDate();
    }

    public void initDate() {
        for (int i = 0; i < list.size(); i++) {
            getIsSelected().put(i, false);
        }
    }

    public static HashMap<Integer, Boolean> getIsSelected() {
        return isSelected;
    }


    public void setDatas(List<HouseListBean> mDatas) {
        this.list = mDatas;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        this.context = parent.getContext();
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_house_item, parent, false);
        // 实例化viewholder
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        // 绑定数据
        holder.check.setTag(position);
        holder.check.setChecked(getIsSelected().get(position));
        holder.check.setOnCheckedChangeListener(new CheckBoxChangedListener());
        final HouseListBean houseListBean = list.get(position);
        holder.mTvDescribe.setText(houseListBean.getSkuName());
        holder.tvRoomAddress.setText(houseListBean.getAddress());

            holder.tvRoomPrice.setText(houseListBean.getRoomPrice());
            String status =  houseListBean.getRoomStatus();
            if (status.equals("1")){
                holder.tvRoomState.setText("已上线");
            }else {
                Resources resources = context.getResources();
                Drawable drawable=resources.getDrawable(R.drawable.bg_care);
                holder.tvRoomState.setBackground(drawable);
                holder.tvRoomState.setText("脏房");
            }
    }


    @Override
    public int getItemCount() {
        return list != null ? list.size() : 0;
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView mTvDescribe;
        TextView tvRoomState;
        TextView tvRoomPrice;
        TextView tvRoomAddress;
        TextView tvDefaultPrice;
        CheckBox check;

        public ViewHolder(View v) {
            super(v);
            mTvDescribe = v.findViewById(R.id.tv_roomDescribe);
            tvRoomAddress = v.findViewById(R.id.tv_roomAddress);
            tvRoomPrice = v.findViewById(R.id.tv_roomPrice);
            tvRoomState = v.findViewById(R.id.tv_roomState);
            tvDefaultPrice = v.findViewById(R.id.tv_default_price);
            check = v.findViewById(R.id.check);
            check = v.findViewById(R.id.check);
        }
    }


    //CheckBox选择改变监听器
    private final class CheckBoxChangedListener implements CompoundButton.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(CompoundButton cb, boolean isChecked) {
            int position = (Integer) cb.getTag();
            getIsSelected().put(position, isChecked);
            HouseListBean shoppingBean = list.get(position);
            shoppingBean.setChoosed(isChecked);

            //如果所有全部被选中，则全选按钮也默认被选中
            mHandler.sendMessage(mHandler.obtainMessage(1, isAllSelected()));
        }
    }


    /**
     * @return true所有条目全部被选中 false还有条目没有被选中
     */
    private boolean isAllSelected() {
        boolean flag = true;
        for (int i = 0; i < list.size(); i++) {
            if (!getIsSelected().get(i)) {
                flag = false;
                break;
            }
        }
        return flag;
    }
}
